package agan.rpc.proxy;

import agan.rpc.protocol.HttpClient;
import agan.rpc.protocol.Invocation;

import java.lang.reflect.Proxy;

public class ProxyFactory {

    public static <T>T getProxy(Class interfaceClass) {
        Object proxy = Proxy.newProxyInstance(ProxyFactory.class.getClassLoader(), new Class[]{interfaceClass}, (proxy1, method, args) -> {
            // 使用反射来创建消息对象
            Invocation invocation = new Invocation(interfaceClass.getName(), method.getName(),
                    method.getParameterTypes(), args);
            HttpClient client = new HttpClient();
            return client.send("localhost", 8080, invocation);
        });
        return (T)proxy;
    }
}
